package com.school.chapter02.graph_;

//邻接矩阵有向图的实现
public class MatrixDG {
    int size ;
    char[] vertexs;
    int[][] matrix;

    public MatrixDG(char[] vertexs, char[][] edegs) {
        size = vertexs.length;
        this.vertexs = vertexs;
        matrix = new int[size][size];

        for (char[] c :
                edegs) {
            int p1 = getPosition(c[0]);
            int p2 = getPosition(c[1]);
            matrix[p1][p2] = 1;

        }
    }

    public void print(){
        for(int[] i:matrix){
            for(int j:i){
                System.out.print(j+" ");
            }
            System.out.println();
        } }
    private int getPosition(char ch) {
        for(int i=0; i<vertexs.length; i++)
            if(vertexs[i]==ch)
                return i;
        return -1;
    }

    //有向图的测试
    public static void main(String[] args) {
        char[] vexs = {'A', 'B', 'C', 'D', 'E', 'F', 'G','H','I','J','K'};
        char[][] edges = new char[][]{
                {'A', 'C'}, {'A', 'D'}, {'A', 'F'},
                {'B', 'C'}, {'C', 'D'}, {'E', 'G'},
                {'D', 'G'},{'I','J'}, {'J','G'},};
        MatrixDG pG;
        pG = new MatrixDG(vexs, edges);
        pG.print();
    }
}
